package com.team.mp.util;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;

public class AliSmsUtil {
    /**
     * 阿里云的AccessKey
     */
    private static final String KEY = "LTAI5tM1kdcVRgR73UjpETxM";
    /**
     * 阿里云的AccessKey Secret
     */
    private static final String SECRET = "mYxdY9kC5XCXCi2CTOp6qpU4nfuh8v";

    /**
     * 注册验证码模板
     */
    public static final String REGISTER = "SMS_114390520";
    /**
     * 登录验证码模板
     */
    public static final String LOGIN = "SMS_115250125";
    /**
     * 密码找回验证码模板
     */
    public static final String FIND = "SMS_177258097";


    private static Client client;

    static {
        Config config = new Config()
                .setAccessKeyId(KEY)
                .setAccessKeySecret(SECRET);
        try {
            client = new Client(config);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 发送验证码
     *
     * @param phone 手机号
     * @param code  验证码的值
     * @param tem   验证码短信模板
     */
    public static boolean sendMsg(String tem, String phone, int code) {
        //1.实例化 SMS请求对象
        SendSmsRequest request = new SendSmsRequest()
                .setPhoneNumbers(phone)
                .setSignName("来自邢朋辉的短信")
                .setTemplateCode(tem)
                .setTemplateParam("{\"code\":\"" + code + "\"}");
        //2.发送短信 获取响应对象
        RuntimeOptions runtime = new RuntimeOptions();
        try {
            SendSmsResponse response = client.sendSmsWithOptions(request, runtime);
            return response.getBody().getCode().equals("OK");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
